# 打开数据库连接
import pymysql


class FoodDao:

    def __init__(self):
        self.__db = pymysql.connect(host='gz-cynosdbmysql-grp-l1ybiknn.sql.tencentcdb.com',
                                    port=21996,
                                    user='root',
                                    password='',
                                    database='cremb'
                                    )

        # 使用 cursor() 方法创建一个游标对象 cursor
        self.__cursor = self.__db.cursor()

    def selectById(self, itemId):
        sql1 = "select * from eb_store_product where id=%s;"
        try:
            # 执行sql语句
            self.__cursor.execute(sql1, itemId)
            # 获取所有记录列表
            results = self.__cursor.fetchall()
            # print(results)
            if len(results) > 0:
                return True
            else:
                return False
        except Exception as e:
            # 如果发生错误则回滚
            print(f"查询{itemId}失败")
            print(e)
            return False

    def close(self):
        # 关闭数据库
        self.__cursor.close()
        self.__db.close()
